<?php

// Check to ensure this file is included in Joomla!
defined ( '_JEXEC' ) or die ( 'Restricted access' );

class _2checkout extends JObject {
	
	var $ipn_data = array ();
	var $config;
	
	function __construct($config) {
		
		if (! isset ( $config->sid )) {
			$config->payment_routine = '';
			$config->sid = '';
			$config->x_receipt_link_url = '';
			$config->demo = 1;
			$config->secret_word = '';
			
		}
//		else{
//			$config->verify    =
//                md5(
//                    $config->sid.
//                    $config->twocheckout_id.
//                    $price
//                );
//		}
		$this->config = $config;
		$this->config->server = $this->config->payment_routine;
		$this->c_name        = '';
        $this->c_description = '';
        $this->c_tangible    = 'N';
	}
	
	function config_form() {
		global $mainframe, $option;
		/* Fill in the config tag below. Note that need not write the form */
		?>
<tr>
	<td class="key"><?php
		echo JText::_ ( '2Chekout Seller ID' );
		?></td>
	<td><input type="text" name="_2checkout[sid]" size="80"
		value="<?php
		echo $this->config->sid;
		?>"></td>
</tr>
<tr>
	<td class="key"><?php
		echo JText::_ ( '2CO Payment routine' );
		?></td>
	<td><input type="text" name="_2checkout[payment_routine]" size="80"
		value="<?php
		echo $this->config->payment_routine;
		?>"><br>
	      <?php
		echo JText::_ ( 'Recommended' );
		?>: <b>https://www.2checkout.com/checkout/purchase</b>
	</td>
</tr>

<tr>
	<td class="key"><?php
		echo JText::_ ( '2Chekout receipt link URL.' );
		?></td>
	<td><input type="text" name="_2checkout[x_receipt_link_url]" size="80"
		value="<?php
		echo $this->config->x_receipt_link_url;
		?>"><br> 
	      (<?php
		echo JText::_ ( 'Enter the return URL here. The return URL for should be:' );
		?> <b><?php
		echo JURI::root () . "index.php?option=com_jajobboar&view=japayments&task=payment_notice&payment_method=_2checkout";
		?></b>
	<br>
	      <?php
		echo JText::_ ( 'This setting overwrites the \'direct return\' URL set in the Look and Feel section your 2CO account.' );
		?>)
      </td>
</tr>

<tr>
	<td class="key"><?php
		echo JText::_ ( 'Demo Mode (Y/N)' );
		?></td>
	<td><input type="radio" name="_2checkout[demo]" value="0"
		<?php
		if ($this->config->demo == '0') {
			echo " checked ";
		}
		?>><?php
		echo JText::_ ( 'No' );
		?> 
	   <input type="radio" name="_2checkout[demo]" value="1"
		<?php
		if ($this->config->demo == '1') {
			echo " checked ";
		}
		?>><?php
		echo JText::_ ( 'Yes' );
		?>
	  </td>
</tr>
<tr>
	<td class="key"><?php
		echo JText::_ ( '2CO Secret Word' );
		?></td>
	<td><input type="text" name="_2checkout[secret_word]" size="50"
		value="<?php
		echo $this->config->secret_word;
		?>"> <br><?php
		echo JText::_ ( '(This is the secret word that is entered under the Look & Feel section of your 2CO account)' );
		?>
      </td>
</tr>

<input name="name" value="_2checkout" type="hidden">
<?php
	}
	
	function more_info() {
		global $option;
		?>
<fieldset><legend><?php
		echo JText::_ ( 'More info' );
		?></legend>
<p><b><?php
		echo JText::_ ( '2Checkout payment plugin configuration' );
		?> </b>
<br />
----------------------------------------- <br />
			  <?php
		echo JText::_ ( 'CONFIUGURATION OF ACCOUNT' )?>
			  <br />


<ol>
	<li>
			  		<?php
		echo JText::_ ( 'Login into your 2Checkout account:' )?>
			  		<div style="padding-left: 10px;"><a target="_blank"
		href="https://www2.2checkout.com/2co/login">https://www2.2checkout.com/2co/login</a>
	</div>
	</li>
	<li>
			  		<?php
		echo JText::_ ( 'Click to "Look&Feel Setttings" link.' )?>
			  		<div style="padding-left: 10px;"><b><?php
		echo JText::_ ( 'Set' )?></b>
	<br />
		  				<?php
		echo JText::_ ( 'Approved URL:' )?>
		  				<br />
	<a target="_blank"
		href="<?php
		echo JRoute::_ ( JURI::root () . "index.php?option=$option&view=japayment&task=payment_notify&pay_method=" . $this->config->name )?>"><?php
		echo JRoute::_ ( JURI::root () . "index.php?option=$option&view=japayment&task=payment_notify&pay_method=" . $this->config->name )?></a>
	<br />
					   <?php
		echo JText::_ ( 'Pending URL:' )?>
					   <br />
	<a target="_blank"
		href="<?php
		echo JRoute::_ ( JURI::root () . "index.php?option=$option&view=japayment&task=payment_notify&pay_method=" . $this->config->name )?>"><?php
		echo JRoute::_ ( JURI::root () . "index.php?option=$option&view=japayment&task=payment_notify&pay_method=" . $this->config->name )?></a>
	<br />
	<br />
	<b> <?php
		echo JText::_ ( 'Your Secret Word:' )?></b> <br />
						 <?php
		echo JText::_ ( 'set to any value you like. the same value must be entered	in Payment 2Checkout plugin settings on this page' )?>
							
			  		</div>
	</li>
</ol>

<b><?php
		echo JText::_ ( 'Note:' )?> </b> 
			  <?php
		echo JText::_ ( 'The script requires a C20 version 2 account. C2O allows only 1 account per website, so if you do not have a C2O account for this website, you will need to register a new C2O account to use this payment option.' );
		?>
			</p>
</fieldset>
<?php
	}
	
	/**
	 * Process notification post from paypal
	 *
	 */
	function check_payment() {
		
		//Get 2Checkout config
		$db = JFactory::getDBO ();
		$query = "SELECT * FROM #__ja_paymentconfig WHERE name='_2checkout'";
		$db->setQuery ( $query );
		$payment = $db->loadObject ();
		$payment->datas = new JParameter ( $payment->datas );
		
		global $jbconfig, $mainframe;
		$postipn = $this->toParamsString ( $_POST );
		global $label;
		
		if (JRequest::getVar ( 'key' ) != '') {
			
			$order_number = JRequest::getVar ( 'order_number' );
			//$order_number = _2CO_SID."-".$order_number;
			//.Demo mode:The order number used to create the Hash is forced to equal 1. This designates that the order is a demo order.
			if ($payment->datas->get ( 'demo' ) == '1') {
				$hash_order_number = 1;
			} else {
				$hash_order_number = $order_number;
			}
			$credit_card_processed = JRequest::getVar ( 'credit_card_processed' ); // Y = successfull. K = pending
			$total = JRequest::getVar ( 'total' );
			$product_id = JRequest::getVar ( 'product_id' ); // c2o product id
			$quantity = JRequest::getVar ( 'quantity' ); // quantity
			$merchant_product_id = JRequest::getVar ( 'merchant_product_id' ); //
			$cart_order_id = JRequest::getVar ( 'cart_order_id' );
			$product_type = substr ( $cart_order_id, 0, 1 ); // 'P' or 'S' or 'C'
			$transaction_id = substr ( $cart_order_id, 1 );
			
			$x_MD5_Hash = strtolower ( JRequest::getVar ( 'key' ) ); // md5 (secret word + vendor number + order number + total)
			

			// md5 (secret word + vendor number + order number + total)
			$md5_str = $payment->datas->get ( 'secret_word' ) . $payment->datas->get ( 'sid' ) . $hash_order_number . $total;
			$hash = md5 ( $md5_str );
			
			if (strcmp ( $hash, $x_MD5_Hash ) == 0) {
				$sql = "SELECT count(*) FROM #__ja_transactions WHERE txn_id='$order_number'";
				$db->setQuery ( $sql );
				$total = $db->loadResult ();
				if ($total) {
					return false;
				} else {
					$db->setQuery ( "SELECT * FROM #__ja_transactions WHERE id = " . $transaction_id . ";" );
					$ret = $db->loadObject ();
					$ret->txn_id = $order_number;
					if ($credit_card_processed == 'Y')
						$ret->completed = 1;
					if ($credit_card_processed == 'K')
						$ret->completed = 0;
					$ret->datas = $this->ipn_data;
					return $ret;
				}
			
			} else {
				return false;
			}
		
		} else
			return false;
	
	}
	
	function toParamsString($vars) {
		$postipn = "";
		foreach ( $vars as $ipnkey => $ipnval ) {
			$this->ipn_data ["$ipnkey"] = $ipnval;
			$postipn .= '&' . @$ipnkey . '=' . urlencode ( @$ipnval );
		} // Notify string
		return $postipn;
	}
	
	function payment_return() {
		?>
<div id="Mod18" class="jamod module">
<div>
<div>
<div>

<div class="jamod-content" style="overflow: hidden;">
					<?php
		echo JText::_ ( "Your 2checkout payment has been completed successfully." )?><br />
					<?php
		echo JText::_ ( "You will receive an email with information about the status of your transaction within minutes." )?><br />
					<?php
		echo JText::_ ( "If you do not receive the email within 20 minutes, please <b>contact us</b>." )?><br />
</div>
</div>
</div>
</div>
</div>

<?php
	$ret = new stdClass();
	return $ret;
	}
	
	function payment_cancel() {
		JError::raiseNotice ( 1, JText::_ ( "You have cancelled the 2checkout payment or the payment was not completed." ) );
		JError::raiseNotice ( 1, JText::_ ( "Please feel free to contact us if your experience was not as it should be." ) );
	}
	
	function assign_params($vars, $values, $payment) {
		
		$vars ['c_name'] = $values ['item_name'];
		$vars ['cart_order_id'] = $values ['item_number'];
		$vars ['c_prod'] = $values ['product_id'];
		$vars ['total'] = $values ['amount'];
		$vars ['c_price'] = $values ['product_price'];
		$vars ['x_receipt_link_url'] = $payment->config->x_receipt_link_url;
		if ($payment->config->demo)
			$vars ['demo'] = 'Y';
		return $vars;
	
	}

}
?>